"""
文件名: MemberInterface.py
作用: 录入会员模块的接口,每一个接口都有一个对应的方法,该方法用于测试用例
作者: 健哥
"""

from Untils.SendMethod import SendMethod
from Untils.GetKeyword import GetKeyword


class MemberInterface:
    def __init__(self):
        self.url = 'http://139.159.146.104:8083'

    def get_auth_code(self, telephone):
        """
        获取验证码接口
        :param telephone: 手机号
        :return:
        """
        # 请求地址
        url = self.url + "/sso/getAuthCode"
        # 请求数据
        payload = {'telephone': telephone}
        return SendMethod.send_method(method='get', url=url, params=payload)

    def verification_get_data(self, telephone):
        """
        获取验证码
        :param telephone: 手机号
        :return:验证码的值
        """
        return GetKeyword.get_keyword(self.get_auth_code(telephone), 'data')

    def member_register(self, username, password, telephone):
        """
        会员注册接口
        :param username: 用户名
        :param password: 用户密码
        :param telephone: 手机号
        :return:
        """
        # 请求地址
        url = self.url + "/sso/register"
        # 请求数据
        payload = {
            'username': username,
            'password': password,
            'telephone': telephone,
            'authCode': self.verification_get_data(telephone)
        }
        return SendMethod.send_method(method='post', url=url, data=payload)

    def member_login(self, username, password):
        """
        会员登录接口
        :param username: 用户名
        :param password: 密码
        :return:
        """
        # 请求地址
        url = self.url + "/sso/login"
        # 请求数据
        payload = {
            'username': username,
            'password': password
        }
        return SendMethod.send_method(method='post', url=url, data=payload)

    def get_token(self, username, password):
        """
        获取会员登录token
        :param username: 用户名
        :param password: 密码
        :return: 拼接后的token请求头信息
        """
        token = GetKeyword.get_keyword(self.member_login(username, password), 'token')
        return {"Authorization": f'Bearer {token}'}

    def refresh_token(self, username, password):
        """
        刷新token
        :return:
        """
        # 请求地址
        url = self.url + "/sso/refreshToken"
        return SendMethod.send_method(method='get', url=url, headers=self.get_token(username, password))

    def modification(self,authCode,password,telephone):
        '''
        修改密码
        :return:
        '''

        url = self.url + "/sso/updatePassword"
        payload = {
            'authCode':authCode,
            'password':password,
            'telephone':telephone
        }

        SendMethod.send_method(method='post',url=url,headers=payload)

if __name__ == '__main__':
    member = MemberInterface()
    authCode = member.get_auth_code(telephone='13812345678')
    # print(member.member_register(username='王老吉', password='123456', telephone='13812345678'))
    # print(member.member_login(username='王老吉', password='123456'))
    # print(member.get_token(username='王老吉', password='123456'))
    # print(member.refresh_token(username='王老吉', password='123456'))
    member.modification(authCode=authCode,password='123456', telephone='13812345678')
